Imports System.Data.SqlClient

Public Class ComboBoxDemo


    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        If (ComboBox3.SelectedItem <> "") Then
            MsgBox("Bạn chọn ca sĩ: " & ComboBox1.SelectedItem)
        End If
    End Sub

    Private Sub ComboBoxDemo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim folder As String() = System.IO.Directory.GetDirectories("C:\Windows")
        ComboBox2.DataSource = folder

        Dim arr As New ArrayList
        arr.Add(New KeyItem("Nha sach Minh Khai", "MK"))
        arr.Add(New KeyItem("Microsoft Corp.", "MS"))
        arr.Add(New KeyItem("Yahoo Inc.", "YH"))
        arr.Add(New KeyItem("Google", "GG"))

        ComboBox3.DataSource = arr
        ComboBox3.DisplayMember = "Name"
        ComboBox3.ValueMember = "Value"

        'Database
        Dim conn As New SqlConnection()
        conn.ConnectionString = " user id=sa;pwd=;data source=.;initial catalog=Northwind"
        Dim da As New SqlDataAdapter("SELECT ProductID, ProductName FROM Products", conn)
        Dim table As New DataTable
        da.Fill(table)

        ComboBox4.DataSource = table
        ComboBox4.DisplayMember = "ProductName"
        ComboBox4.ValueMember = "ProductID"

    End Sub



    Private Sub ComboBox3_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox3.SelectionChangeCommitted
        MsgBox(String.Format("Bạn chọn {0} - Mã số {1}", ComboBox3.Text, ComboBox3.SelectedValue))
    End Sub

    Private Sub ComboBox4_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox4.SelectedIndexChanged
        MsgBox(String.Format("Bạn chọn {0} - Mã số {1}", ComboBox4.Text, ComboBox4.SelectedValue))
    End Sub

End Class

Class KeyItem
    'Khai baùo bieán cuïc boä
    Private _strValue As String = ""
    Private _strName As String = ""

    Sub New(ByVal sName As String, ByVal sValue As String)
        _strName = sName
        _strValue = sValue
    End Sub
    Public Property Name() As String
        Get
            Return _StrName
        End Get
        Set(ByVal Value As String)
            _StrName = value
        End Set
    End Property
    ' Khai baùo thuoäc tính Value
    Public Property Value() As String
        Get
            Return _StrValue
        End Get
        Set(ByVal Value As String)
            _StrValue = value
        End Set
    End Property

End Class

